package com.webside.staticHtml.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.webside.exception.SystemException;
import com.webside.util.Common;
import com.webside.util.PageUtil;

@Controller
@Scope("prototype")
@RequestMapping("/pagestatic/")
public class StaticHtmlController {
	
	@RequestMapping("generatepage.html")
	public String listUI(Model model, HttpServletRequest request) {
		try
		{
			PageUtil page = new PageUtil();
			if(request.getParameterMap().containsKey("page")){
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			}
			model.addAttribute("page", page);
			return Common.BACKGROUND_PATH + "/statichtml/list";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	//静态化
		@RequestMapping("static.html")
		@ResponseBody
		public Object deleteBatch(String ids) {
			Map<String, Object> result = new HashMap<String, Object>();
				if(StaticHtmlController.executeHttpPost("")!=null)
				{
					result.put("success", true);
					result.put("data", null);
					result.put("message", "静态化成功");
				}else
				{
					result.put("success", false);
					result.put("data", null);
					result.put("message", "静态化失败");
				}
			return result;
		}
		public static Object executeHttpPost(String path) {
			JSONObject resultJsonObject = null;
			JSONArray resultJsonArray = null;
			HttpClient httpClient = new DefaultHttpClient();
			try {
				HttpGet httpRequest = new HttpGet(path);
				httpRequest.setHeader("Content-Type", "application/json; charset=utf-8");
				HttpResponse httpResponse = httpClient.execute(httpRequest);
				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					HttpEntity httpEntity = httpResponse.getEntity();
					if (httpEntity != null) {
						String result = EntityUtils.toString(httpEntity, "UTF-8");
						if(result.indexOf("{")==0){
							resultJsonObject = JSONObject.parseObject(result);
						}else{
							resultJsonArray= JSONArray.parseArray(result);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				httpClient.getConnectionManager().shutdown();
			}
			if(resultJsonObject!=null){
				return resultJsonObject;
			}else{
				return resultJsonArray;
			}
			
		}
}
